package com.tjec.project.businessWorkbench.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import javax.validation.constraints.Null;

import com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item;
import com.alibaba.fastjson.JSONArray;
import com.tjec.common.enums.BusinessStatus;
import com.tjec.common.enums.CatalogType;
import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.ReflectUtils;
import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.project.businessWorkbench.domain.BmsProjectDocCatalog;
import com.tjec.project.businessWorkbench.service.IBmsImportExcelService;
import com.tjec.project.businessWorkbench.service.IBmsProjectDocCatalogService;
import com.tjec.project.mdmSynchronous.dao.AgencyNoDao;
import com.tjec.project.mdmSynchronous.dao.BusinessMigrateDao;
import com.tjec.project.mdmSynchronous.dao.CostNoDao;
import com.tjec.project.mdmSynchronous.dao.ProjectContractDao;
import com.tjec.project.mdmSynchronous.dao.ReportNoDao;
import com.tjec.project.portal.service.IBmsMsgService;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsLeaderMember;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsLeaderMemberService;
import com.tjec.project.projectWorkbench.domain.BmsAgencyNo;
import com.tjec.project.projectWorkbench.domain.BmsBusinessOper;
import com.tjec.project.projectWorkbench.domain.BmsCostNo;
import com.tjec.project.projectWorkbench.domain.BmsProject;
import com.tjec.project.projectWorkbench.domain.BmsProjectBusinessDto;
import com.tjec.project.projectWorkbench.domain.BmsProjectMember;
import com.tjec.project.projectWorkbench.domain.BmsReportNo;
import com.tjec.project.projectWorkbench.mapper.BmsAgencyNoMapper;
import com.tjec.project.projectWorkbench.mapper.BmsBusinessOperMapper;
import com.tjec.project.projectWorkbench.mapper.BmsCostNoMapper;
import com.tjec.project.projectWorkbench.mapper.BmsProjectMapper;
import com.tjec.project.projectWorkbench.mapper.BmsProjectMemberMapper;
import com.tjec.project.projectWorkbench.mapper.BmsReportNoMapper;
import com.tjec.project.projectWorkbench.service.IBmsBusinessOperService;
import com.tjec.project.projectWorkbench.service.IBmsProjectMemberService;
import com.tjec.project.projectWorkbench.service.IBmsProjectService;
import com.tjec.project.system.domain.BmsMdArchiveTemplate;
import com.tjec.project.system.domain.BmsMdBusinessCategory;
import com.tjec.project.system.domain.BmsServiceArea;
import com.tjec.project.system.domain.SysDictData;
import com.tjec.project.system.domain.SysUser;
import com.tjec.project.system.domain.vo.BmsMdBusinessCategoryVO;
import com.tjec.project.system.mapper.BmsMdBusinessCategoryMapper;
import com.tjec.project.system.mapper.BmsServiceAreaMapper;
import com.tjec.project.system.mapper.SysDictDataMapper;
import com.tjec.project.system.mapper.SysUserMapper;
import com.tjec.project.system.service.IBmsMdArchiveTemplateService;
import com.tjec.project.system.service.IBmsMdBusinessCategoryService;
import com.tjec.project.workflow.domain.BmsWorkflow;
import com.tjec.project.workflow.mapper.BmsWorkflowMapper;

import org.apache.poi.hssf.dev.ReSave;
import org.apache.poi.hssf.record.OldCellRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import com.tjec.project.businessWorkbench.mapper.BmsBusinessMapper;
import com.tjec.project.businessWorkbench.domain.BmsBusiness;
import com.tjec.project.businessWorkbench.service.IBmsBusinessService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.InitBinder;

/**
 * 业务一览Service业务层处理
 *
 * @author pangyongfeng
 * @date 2020-04-20
 */
@Service
public class BmsImportExcelServiceImpl implements IBmsBusinessService {

	private static final Logger log = LoggerFactory.getLogger(BmsImportExcelServiceImpl.class);

	@Autowired
	private BmsBusinessMapper bmsBusinessMapper;

	@Autowired
	private IBmsProjectMemberService bmsProjectMemberService;

	@Autowired
	private IBmsMdBusinessCategoryService bmsMdBusinessCategoryService;

	@Autowired
	private IBmsProjectDocCatalogService bmsProjectDocCatalogService;

	@Autowired
	private IBmsImportExcelService bmsImportExcelService;

	@Autowired
	private SysUserMapper sysUserMapper;

	@Autowired
	private IBmsMsgService bmsMsgService;

	@Autowired
	private BmsProjectMapper bmsProjectMapper;

	@Autowired
	private IBmsLeaderMemberService bmsLeaderMemberService;

	@Autowired
	private BmsAgencyNoMapper bmsAgencyNoMapper;

	@Autowired
	private BmsCostNoMapper bmsCostNoMapper;

	@Autowired
	private BmsReportNoMapper bmsReportNoMapper;

	@Autowired
	private IBmsProjectService bmsProjectService;

	@Autowired
	private BmsWorkflowMapper bmsWorkflowMapper;

	@Autowired
	private SysDictDataMapper sysDictDataMapper;

	@Autowired
	private BmsServiceAreaMapper bmsServiceAreaMapper;

	@Autowired
	private BmsMdBusinessCategoryMapper bmsMdBusinessCategoryMapper;

	@Autowired
    private AgencyNoDao agencyNoDao;

	@Autowired
    private CostNoDao costNoDao;

	@Autowired
    private ReportNoDao reportNoDao;

	@Autowired
	private BusinessMigrateDao businessMigrateDao;

	@Autowired
	private IBmsBusinessOperService bmsBusinessOperService;

	@Autowired
	private IBmsMdArchiveTemplateService bmsMdArchiveTemplateService;
	/**
	 * 查询业务一览
	 *
	 * @param id 业务一览ID
	 * @return 业务一览
	 */
	@Override
	public BmsBusiness selectBmsBusinessByGuid(String guid) {
		return bmsBusinessMapper.selectBmsBusinessByGuid(guid);
	}

	/**
	 * 查询业务一览列表
	 *
	 * @param bmsBusiness 业务一览
	 * @return 业务一览
	 */
	@Override
	public List<BmsBusiness> selectBmsBusinessList(BmsBusiness bmsBusiness) {
		if(StringUtils.isNotBlank(bmsBusiness.getStatus()) && "07".equals(bmsBusiness.getStatus())) {
			bmsBusiness.setStatus("");
			bmsBusiness.setCancelStatus("1");
		}
		List<BmsBusiness> res = bmsBusinessMapper.selectBmsBusinessList(bmsBusiness);
		/*
		 * if(res!=null && res.size()>0) { res.forEach(item->{ String reportNoApply =
		 * item.getReportNoApply(); if(reportNoApply!=null && !"".equals(reportNoApply))
		 * { if("1".equals(reportNoApply)) {
		 *
		 * String agencyNo= item.getAgencyNo(); String sectionName=
		 * item.getSectionName(); String agencyStampTime= item.getAgencyStampTime();
		 *
		 * if(agencyNo!=null && !"".equals(agencyNo)) { List<BmsReportNo> reportList =
		 * new ArrayList<BmsReportNo>(); String[] nos = agencyNo.split(";;"); for(int
		 * i=0;i<nos.length;i++) { BmsReportNo reportNo = new BmsReportNo();
		 * reportNo.setReportNo(nos[i]);
		 * reportNo.setReportName(sectionName.split(";;")[i]);
		 * if(agencyStampTime.split(";;").length==0) { reportNo.setStampTime(""); }else
		 * if(agencyStampTime.split(";;").length==1) { if(i==0) {
		 * reportNo.setStampTime(agencyStampTime.split(";;")[0]); }else {
		 * reportNo.setStampTime(""); } }else {
		 * reportNo.setStampTime(agencyStampTime.split(";;")[i]); }
		 *
		 * reportList.add(reportNo); } item.setReportList(reportList); } } }
		 *
		 * }); }
		 */
		return res;
	}

	/**
	 * 新增业务一览
	 *
	 * @param bmsBusiness 业务一览
	 * @return 结果
	 */
	@Transactional
	@Override
	public int insertBmsBusiness(BmsBusiness bmsBusiness) {



		BmsMdBusinessCategory businessType = bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryByGuId(bmsBusiness.getBusinessCategoryId());
		String reportNoApply = businessType.getReportNoApply();
		String businessStatus = bmsBusiness.getStatus();
		String businessName = bmsBusiness.getName();

		if(StringUtils.isNotBlank(reportNoApply) && StringUtils.isNotBlank(businessStatus) && "07".equals(businessStatus)) {
			if("1".equals(reportNoApply)) {
				reportNoDao.updateReportNoStatus(bmsBusiness.getGuid(), "作废");
			}else if("2".equals(reportNoApply)) {
				agencyNoDao.updateAgencyNoStatus(bmsBusiness.getGuid(), "作废");
			}else if("3".equals(reportNoApply)) {
				costNoDao.updateCostNoStatus(bmsBusiness.getGuid(), "作废");
			}
		}

		if("07".equals(businessStatus)) {
			bmsBusiness.setName("(作废)"+businessName);
		}


		bmsBusiness.setCreateTime(DateUtils.getNowDate());
		String createBy = SecurityUtils.getLoginUser().getUser().getUserId();
		bmsBusiness.setCreateBy(createBy);
		if (null != bmsBusiness.getTenderMembers() && bmsBusiness.getTenderMembers().length > 0) {
			bmsBusiness.setTenderMember(StringUtils.arrayToString(bmsBusiness.getTenderMembers()));
		}
		// 添加业务编号,业务编号当前时间yyyyMMdd+随机4位数
		String sectionNumber = bmsBusinessMapper
				.selectBmsBusinessTodayMaxSectionNumber(DateUtils.dateTimeNow(DateUtils.YYYYMMDD));
		bmsBusiness.setSectionNumber(sectionNumber);
		int result=0;
		try {
			result = bmsBusinessMapper.insertBmsBusiness(bmsBusiness);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(result>0) {
			// 项目文档里加一个目录
			BmsProjectDocCatalog bmsProjectDocCatalog = new BmsProjectDocCatalog();
			bmsProjectDocCatalog.setParentId("0");
			bmsProjectDocCatalog.setName(bmsBusiness.getName());
			bmsProjectDocCatalog.setBusinessId(bmsBusiness.getGuid());
			bmsProjectDocCatalog.setSort(1D);
			bmsProjectDocCatalog.setCreateBy(createBy);
			bmsProjectDocCatalogService.insertBmsProjectDocCatalog(bmsProjectDocCatalog);

			bmsProjectDocCatalogService.addDocCatalog(bmsBusiness.getGuid(), CatalogType.ZERO.getCode());
			bmsProjectDocCatalogService.addDocCatalog(bmsBusiness.getGuid(), CatalogType.TWO.getCode());
			bmsProjectDocCatalogService.addDocCatalog(bmsBusiness.getGuid(), CatalogType.THREE.getCode());

			// 导入资料归档模板
			bmsImportExcelService.importTemplte(bmsBusiness.getArchiveTemplateId(), "4", bmsBusiness.getGuid());
		}
		String hisName = "";
		//历史报告号添加到对应表中
		if(StringUtils.isNotBlank(bmsBusiness.getAgencyNo())&&bmsBusiness.getAgencyNo().indexOf("H0701")!=-1){
			BmsAgencyNo bmsAgencyNo = new BmsAgencyNo();
			bmsAgencyNo.setAgencyNo(bmsBusiness.getAgencyNo());
			bmsAgencyNo = bmsAgencyNoMapper.selectBmsAgencyNoHistoryByAgencyNo(bmsAgencyNo);
			bmsAgencyNo.setBusinessId(bmsBusiness.getGuid());
			bmsAgencyNo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
			bmsAgencyNo.setCreateTime(DateUtils.getNowDate());
			bmsAgencyNoMapper.insertBmsAgencyNo(bmsAgencyNo);
			bmsAgencyNoMapper.updateBmsAgencyNoHistoryStatusByAgencyNo(bmsAgencyNo);
			hisName = bmsAgencyNo.getSectionName();
		}
		if(StringUtils.isNotBlank(bmsBusiness.getAgencyNo())&&bmsBusiness.getAgencyNo().indexOf("H0702")!=-1){
			BmsCostNo bmsCostNo = new BmsCostNo();
			bmsCostNo.setCostNo(bmsBusiness.getAgencyNo());
			bmsCostNo = bmsCostNoMapper.selectBmsCostNoHistoryByCostNo(bmsCostNo);
			bmsCostNo.setBusinessId(bmsBusiness.getGuid());
			bmsCostNo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
			bmsCostNo.setCreateTime(DateUtils.getNowDate());
			bmsCostNo.setStampTime(bmsBusiness.getAgencyStampTime());
			bmsCostNo.setCostType(bmsBusiness.getReportType());
			bmsCostNoMapper.insertBmsCostNo(bmsCostNo);
			bmsCostNoMapper.updateBmsCostNoHistoryByStatusCostNo(bmsCostNo);
			hisName = bmsCostNo.getCostName();
		}

		BmsMdArchiveTemplate archiveRes = bmsMdArchiveTemplateService.selectBmsMdArchiveTemplateById(bmsBusiness.getArchiveTemplateId());
		//添加操作记录
		BmsBusinessOper bmsBusinessOper = new BmsBusinessOper();
		bmsBusinessOper.setBusinessId(bmsBusiness.getGuid());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		bmsBusinessOper.setOperPerson(SecurityUtils.getLoginUser().getUser().getNickName());
		bmsBusinessOper.setOperPersonNo(SecurityUtils.getLoginUser().getUser().getJobNo());
		bmsBusinessOper.setBusinessName(bmsBusiness.getName());
		bmsBusinessOper.setProjectId(bmsBusiness.getProjectId());
		bmsBusinessOper.setFieldName("");
		bmsBusinessOper.setBusinessNo(bmsBusiness.getSectionNumber());

		if((StringUtils.isNotBlank(bmsBusiness.getAgencyNo()) && bmsBusiness.getAgencyNo().indexOf("H0701")!=-1) || (StringUtils.isNotBlank(bmsBusiness.getAgencyNo())&&bmsBusiness.getAgencyNo().indexOf("H0702")!=-1)) {
			bmsBusinessOper.setOperType("6");
			if(StringUtils.isNotBlank(bmsBusiness.getTenderLeader())) {
				bmsBusinessOper.setNewParams("业务名称："+bmsBusiness.getName()+"；业务类别："+businessType.getMenuName()+"；业务状态："+bmsBusiness.getStatusName()+"；状态说明："+(bmsBusiness.getStatusRemark()==null?"":bmsBusiness.getStatusRemark())+"；开始日期："+sdf.format(bmsBusiness.getBusinessStartTime())+"；归档模板："+archiveRes.getTemplateName()+"；报告号/招标号："+bmsBusiness.getAgencyNo()+"；报告名称/标段名称："+hisName+"；业务编号："+bmsBusiness.getSectionNumber()+"；招标组组长："+bmsBusiness.getTenderLeaderName()+"；招标组组员："+bmsBusiness.getTenderMemberNames()+"；");
			}else {
				bmsBusinessOper.setNewParams("业务名称："+bmsBusiness.getName()+"；业务类别："+businessType.getMenuName()+"；业务状态："+bmsBusiness.getStatusName()+"；状态说明："+(bmsBusiness.getStatusRemark()==null?"":bmsBusiness.getStatusRemark())+"；开始日期："+sdf.format(bmsBusiness.getBusinessStartTime())+"；归档模板："+archiveRes.getTemplateName()+"；报告号/招标号："+bmsBusiness.getAgencyNo()+"；报告名称/标段名称："+hisName+"；业务编号："+bmsBusiness.getSectionNumber());
			}

		}else {
			bmsBusinessOper.setOperType("1");
			if(StringUtils.isNotBlank(bmsBusiness.getTenderLeader())) {
				bmsBusinessOper.setNewParams("业务名称："+bmsBusiness.getName()+"；业务类别："+businessType.getMenuName()+"；业务状态："+bmsBusiness.getStatusName()+"；状态说明："+(bmsBusiness.getStatusRemark()==null?"":bmsBusiness.getStatusRemark())+"；开始日期："+sdf.format(bmsBusiness.getBusinessStartTime())+"；归档模板："+archiveRes.getTemplateName()+"；招标组组长："+bmsBusiness.getTenderLeaderName()+"；招标组组员："+bmsBusiness.getTenderMemberNames()+"；业务编号："+bmsBusiness.getSectionNumber()+"；");
			}else {
				bmsBusinessOper.setNewParams("业务名称："+bmsBusiness.getName()+"；业务类别："+businessType.getMenuName()+"；业务状态："+bmsBusiness.getStatusName()+"；状态说明："+(bmsBusiness.getStatusRemark()==null?"":bmsBusiness.getStatusRemark())+"；开始日期："+sdf.format(bmsBusiness.getBusinessStartTime())+"；归档模板："+archiveRes.getTemplateName()+"；业务编号："+bmsBusiness.getSectionNumber());
			}
		}
		bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);
		return result;
	}

	/**
	 * 修改业务一览
	 *
	 * @param bmsBusiness 业务一览
	 * @return 结果
	 */
	@Transactional
	@Override
	public int updateBmsBusiness(BmsBusiness bmsBusiness) {
		/*BmsMdBusinessCategory businessType = bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryByGuId(bmsBusiness.getBusinessCategoryId());
		String businessStatus = bmsBusiness.getStatus();
		String businessName = bmsBusiness.getName();
		if(businessType!=null && businessType.getReportNoApply()!=null) {
			String reportNoApply = businessType.getReportNoApply();


			if(StringUtils.isNotBlank(reportNoApply)) {
				String eCStatus = "";
				if(StringUtils.isNotBlank(reportNoApply) && "07".equals(businessStatus)) {
					eCStatus="作废";

				}

				if("1".equals(reportNoApply)) {
					reportNoDao.updateReportNoStatus(bmsBusiness.getGuid(), eCStatus);
					if("作废".equals(eCStatus)) {
						bmsReportNoMapper.updateReportByBusinessIdCancel(bmsBusiness.getGuid());
					}else {
						bmsReportNoMapper.updateReportByBusinessId(bmsBusiness.getGuid());
					}
					//bmsCostNoMapper
				}else if("2".equals(reportNoApply)) {
					agencyNoDao.updateAgencyNoStatus(bmsBusiness.getGuid(), eCStatus);
					if("作废".equals(eCStatus)) {
						bmsAgencyNoMapper.updateAgencyByBusinessIdCancel(bmsBusiness.getGuid());
					}else {
						bmsAgencyNoMapper.updateAgencyByBusinessId(bmsBusiness.getGuid());
					}
				}else if("3".equals(reportNoApply)) {
					costNoDao.updateCostNoStatus(bmsBusiness.getGuid(), eCStatus);
					if("作废".equals(eCStatus)) {
						bmsCostNoMapper.updateCostByBusinessIdCancel(bmsBusiness.getGuid());
					}else {
						bmsCostNoMapper.updateCostByBusinessId(bmsBusiness.getGuid());
					}
				}
			}
		}*/
		/*if("07".equals(businessStatus)) {
			bmsBusiness.setName("(作废)"+businessName);
		}else {
			if(StringUtils.isNotBlank(businessName) && businessName.length()>4 && "(作废)".equals(businessName.substring(0, 4))) {
				bmsBusiness.setName(businessName.substring(4));
			}
		}*/


		bmsBusiness.setUpdateTime(DateUtils.getNowDate());

		/*if(SecurityUtils.getAuthentication()!=null) {
			bmsBusiness.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
		}*/

		if (null != bmsBusiness.getTenderMembers() && bmsBusiness.getTenderMembers().length > 0) {
			bmsBusiness.setTenderMember(StringUtils.arrayToString(bmsBusiness.getTenderMembers()));
		}
		BmsBusiness oldBusiness = bmsBusinessMapper.selectBmsBusinessByGuid(bmsBusiness.getGuid());
		//添加操作记录
		BmsBusinessOper bmsBusinessOper = new BmsBusinessOper();
		bmsBusinessOper.setOperType("2");
		bmsBusinessOper.setBusinessNo(oldBusiness.getSectionNumber());
		bmsBusinessOper.setBusinessId(bmsBusiness.getGuid());
		bmsBusinessOper.setProjectId(bmsBusiness.getProjectId());
		bmsBusinessOper.setBusinessName(oldBusiness.getName());
		if(StringUtils.isNotBlank(bmsBusiness.getOperPerson())) {
			bmsBusinessOper.setOperPerson(bmsBusiness.getOperPerson());
		}else {
			bmsBusinessOper.setOperPerson(SecurityUtils.getLoginUser().getUser().getNickName());
		}

		if(StringUtils.isNotBlank(bmsBusiness.getOperPersonNo())) {
			bmsBusinessOper.setOperPersonNo(bmsBusiness.getOperPersonNo());
		}else {
			bmsBusinessOper.setOperPersonNo(SecurityUtils.getLoginUser().getUser().getJobNo());
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		if(StringUtils.isNotBlank(bmsBusiness.getName()) && !(bmsBusiness.getName()).equals(oldBusiness.getName())) {
			bmsBusinessOper.setFieldName("业务名称");
			bmsBusinessOper.setInitParams(oldBusiness.getName());
			bmsBusinessOper.setNewParams(bmsBusiness.getName());
			bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);
		}

		if(StringUtils.isNotBlank(bmsBusiness.getStatus()) && !(bmsBusiness.getStatus()).equals(oldBusiness.getStatus())) {
			bmsBusinessOper.setFieldName("业务状态");

			if("01".equals(oldBusiness.getStatus())) {
				bmsBusinessOper.setInitParams(BusinessStatus.STATUS1.getName());
			}
			if("01".equals(bmsBusiness.getStatus())) {
				bmsBusinessOper.setNewParams(BusinessStatus.STATUS1.getName());
			}

			if("02".equals(oldBusiness.getStatus())) {
				bmsBusinessOper.setInitParams(BusinessStatus.STATUS2.getName());
			}
			if("02".equals(bmsBusiness.getStatus())) {
				bmsBusinessOper.setNewParams(BusinessStatus.STATUS2.getName());
			}

			if("03".equals(oldBusiness.getStatus())) {
				bmsBusinessOper.setInitParams(BusinessStatus.STATUS3.getName());
			}
			if("03".equals(bmsBusiness.getStatus())) {
				bmsBusinessOper.setNewParams(BusinessStatus.STATUS3.getName());
			}

			if("04".equals(oldBusiness.getStatus())) {
				bmsBusinessOper.setInitParams(BusinessStatus.STATUS4.getName());
			}
			if("04".equals(bmsBusiness.getStatus())) {
				bmsBusinessOper.setNewParams(BusinessStatus.STATUS4.getName());
			}

			if("05".equals(oldBusiness.getStatus())) {
				bmsBusinessOper.setInitParams(BusinessStatus.STATUS5.getName());
			}
			if("05".equals(bmsBusiness.getStatus())) {
				bmsBusinessOper.setNewParams(BusinessStatus.STATUS5.getName());
			}

			if("06".equals(oldBusiness.getStatus())) {
				bmsBusinessOper.setInitParams(BusinessStatus.STATUS6.getName());
			}
			if("06".equals(bmsBusiness.getStatus())) {
				bmsBusinessOper.setNewParams(BusinessStatus.STATUS6.getName());
			}

			bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);
		}

		if(StringUtils.isNotBlank(bmsBusiness.getStatusRemark()) && !(bmsBusiness.getStatusRemark()).equals(oldBusiness.getStatusRemark())) {
			bmsBusinessOper.setFieldName("状态说明");
			bmsBusinessOper.setInitParams(oldBusiness.getStatusRemark()==null?"":oldBusiness.getStatusRemark());
			bmsBusinessOper.setNewParams(bmsBusiness.getStatusRemark()==null?"":bmsBusiness.getStatusRemark());
			bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);
		}

		if(bmsBusiness.getBusinessStartTime()!=null && !(bmsBusiness.getBusinessStartTime()).equals(oldBusiness.getBusinessStartTime())) {
			bmsBusinessOper.setFieldName("开始日期");
			bmsBusinessOper.setInitParams(sdf.format(oldBusiness.getBusinessStartTime()));
			bmsBusinessOper.setNewParams(sdf.format(bmsBusiness.getBusinessStartTime()));
			bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);
		}

		if(bmsBusiness.getBusinessEndTime()!=null && !(bmsBusiness.getBusinessEndTime()).equals(oldBusiness.getBusinessEndTime())) {
			bmsBusinessOper.setFieldName("结束日期");
			if(oldBusiness.getBusinessEndTime()!=null) {
				bmsBusinessOper.setInitParams(sdf.format(oldBusiness.getBusinessEndTime()));
			}
			bmsBusinessOper.setNewParams(sdf.format(bmsBusiness.getBusinessEndTime()));
			bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);
		}

		//bmsBusinessOper.setInitParams(JSONArray.toJSON(oldBusiness).toString());
		//bmsBusinessOper.setNewParams(JSONArray.toJSON(bmsBusiness).toString());





		return bmsBusinessMapper.updateBmsBusiness(bmsBusiness);
	}

	/**
	 * 批量删除业务一览
	 *
	 * @param ids 需要删除的业务一览ID
	 * @return 结果
	 */
	@Override
	public int deleteBmsBusinessByGuids(String[] guids) {
		return bmsBusinessMapper.deleteBmsBusinessByIds(guids);
	}

	/**
	 * 删除业务一览信息
	 *
	 * @param id 业务一览ID
	 * @return 结果
	 */
	@Override
	public int deleteBmsBusinessByGuid(String guid) {
		return bmsBusinessMapper.deleteBmsBusinessByGuid(guid);
	}

	/**
	 * 业务迁移
	 *
	 * @param bmsBusiness
	 * @return
	 */
	@Transactional
	@Override
	public int migrationBmsBusiness(BmsBusiness bmsBusiness) {

		BmsBusiness oldBusiness = bmsBusinessMapper.selectBmsBusinessByGuid(bmsBusiness.getGuid());
		BmsProject oldProject = bmsProjectMapper.selectBmsProjectByGuid(oldBusiness.getProjectId());

		int result = 0;
		BmsBusiness bmsBus = this.selectBmsBusinessByGuid(bmsBusiness.getGuid());
		bmsBus.setProjectId(bmsBusiness.getProjectId());
		bmsBus.setOperPerson(bmsBusiness.getOperPerson());
		bmsBus.setOperPersonNo(bmsBusiness.getOperPersonNo());
		result = updateBmsBusiness(bmsBus);

		// 根据项目id获取项目信息
		BmsProject project = bmsProjectMapper.selectBmsProjectByGuid(bmsBusiness.getProjectId());

		// 迁移项目为“招标代理”业务，需把招标组组长，组员自动加入新项目成员和新项目负责人的人员清单管理
		Set<String> engineeringBiddingList = bmsMdBusinessCategoryService.selectEngineeringBidding();
		if (engineeringBiddingList.contains(bmsBus.getBusinessCategoryId())) {
			if (StringUtils.isNotBlank(bmsBus.getTenderMember())) {
				String[] tenderMembers = bmsBus.getTenderMember().split(";");
				for (String tenderMember : tenderMembers) {
					insertBmsProjectMember(project, tenderMember);
				}
			}
			insertBmsProjectMember(project, bmsBus.getTenderLeader());
		}
		// 更新对应迁移流程的项目ID
		bmsWorkflowMapper.updateProjectByBusinessId(bmsBusiness.getGuid(), bmsBusiness.getProjectId());
		if (result > 0) {
			// 调bpm接口，将迁移业务未完成流程在BPM中终止（取消）
			BmsWorkflow bmsWorkflow = new BmsWorkflow();
			bmsWorkflow.setBusinessId(bmsBusiness.getGuid());
			bmsProjectService.cancelInstance(bmsWorkflow);
		}
		log.debug("迁移前项目id——————————————————-"+bmsBusiness.getProjectId());
		log.debug("迁移后项目id——————————————————-"+project.getGuid());
		if(StringUtils.isNotBlank(bmsBusiness.getProjectId()) && StringUtils.isNotBlank(project.getGuid())) {
			log.debug("-------------开始更新oa数据库-------------");
			//更新oa数据库
			System.out.println(bmsBusiness.getGuid());
			System.out.println(project.getGlobalId());
			System.out.println(project.getCode());
			businessMigrateDao.updateProjectCodeAndId(bmsBusiness.getGuid(), project.getGlobalId(), project.getCode());
			log.debug("-------------结束更新oa数据库-------------");
		}

		//添加操作记录
		BmsBusinessOper bmsBusinessOper = new BmsBusinessOper();
		bmsBusinessOper.setOperType("4");
		bmsBusinessOper.setFieldName("项目代码");
		bmsBusinessOper.setBusinessId(bmsBusiness.getGuid());
		bmsBusinessOper.setInitParams(oldProject.getCode());
		bmsBusinessOper.setNewParams(project.getCode());
		if(StringUtils.isNotBlank(bmsBusiness.getOperPerson())) {
			bmsBusinessOper.setOperPerson(bmsBusiness.getOperPerson());
		}else {
			bmsBusinessOper.setOperPerson(SecurityUtils.getLoginUser().getUser().getNickName());
		}

		if(StringUtils.isNotBlank(bmsBusiness.getOperPersonNo())) {
			bmsBusinessOper.setOperPersonNo(bmsBusiness.getOperPersonNo());
		}else {
			bmsBusinessOper.setOperPersonNo(SecurityUtils.getLoginUser().getUser().getJobNo());
		}
		bmsBusinessOper.setProjectId(oldProject.getGuid());
		bmsBusinessOper.setBusinessName(oldBusiness.getName());
		bmsBusinessOper.setBusinessNo(oldBusiness.getSectionNumber());
		bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);
		bmsBusinessOper.setProjectId(project.getGuid());
		bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);

		return result;
	}

	@Override
	public List<BmsBusiness> selectBmsBusinessByProjectId(String projectId) {
		return bmsBusinessMapper.selectBmsBusinessByProjectId(projectId);
	}

	/**
	 * 招标组组长，组员自动加入新项目成员和新项目负责人的人员清单管理
	 *
	 * @param project
	 * @param tenderMember
	 * @return
	 */
	private void insertBmsProjectMember(BmsProject project, String tenderMember) {
		if (StringUtils.isNoneBlank(project.getGuid(), tenderMember)) {
			// 添加项目人员清单
			SysUser sysUser = sysUserMapper.findSysuUserByProjectLeaderNo(project.getProjectLeaderNo());
			if (sysUser != null) {
				boolean leaderFlag = bmsLeaderMemberService.repeatBmsLeaderMember(sysUser.getUserId(), tenderMember);
				if (!leaderFlag) {
					BmsLeaderMember bmsLeaderMember = new BmsLeaderMember();
					bmsLeaderMember.setLeader(sysUser.getUserId());
					bmsLeaderMember.setUserId(tenderMember);
					bmsLeaderMemberService.insertBmsLeaderMember(bmsLeaderMember);
					// 消息通知-推送系统消息给项目负责人，提醒把招标组长和组员加入到了项目负责人的人员清单
					// 获取用户信息
					SysUser user = sysUserMapper.selectUserById(tenderMember);
					String msgType = "业务迁移";
					String content = "系统管理员：" + user.getNickName() + "已被加入到您的人员清单";
					bmsMsgService.insertByReceiverUser(sysUser.getUserId(), msgType, content,true);
				}
			}
			boolean projectFlag = bmsProjectMemberService.repeatBmsProjectMember(project.getGuid(), tenderMember);
			if (!projectFlag) {
				// 添加项目成员
				BmsProjectMember bmsProjectMember = new BmsProjectMember();
				bmsProjectMember.setProjectId(project.getGuid());
				bmsProjectMember.setUserId(tenderMember);
				bmsProjectMember.setRole("1");
				bmsProjectMember.setJoinTime(new Date());
				bmsProjectMemberService.insertBmsProjectMember(bmsProjectMember);
			}
		}
	}

	@Override
	public List<BmsBusiness> selectBusinessNoReportNoList(BmsBusiness bmsBusiness) {
		List<BmsBusiness> list = bmsBusinessMapper.selectBusinessNoReportNoList(bmsBusiness);
		if (list != null && list.size() > 0) {
			Iterator<BmsBusiness> iterator = list.iterator();

			while (iterator.hasNext()) {
				BmsBusiness item = iterator.next();
				String nowReportType = item.getNowReportType();
				String oldReportType = item.getOldReportType();
				if (oldReportType != null && !"".equals(oldReportType)) {
					if (nowReportType != null && !"".equals(nowReportType)) {
						List<String> difflist = new ArrayList<String>();
						String[] oldList = oldReportType.split(";");
						String[] nowList = nowReportType.split(";");

						List<String> lists = Arrays.asList(nowList);

						Arrays.sort(oldList);
						Arrays.sort(nowList);
						if (Arrays.equals(oldList, nowList)) {
							iterator.remove();
						}

						for (String a : oldList) {
							// 判断是否包含
							if (!lists.contains(a)) {
								difflist.add(a);
							}
						}
						item.setReportTypes(difflist);
					} else {
						String[] oldList = oldReportType.split(";");
						item.setReportTypes(Arrays.asList(oldList));
					}
				} else {
					// 如果没有报告类型，则过滤掉
					iterator.remove();
				}
			}
			return list;
		} else {
			return list;
		}
	}

	@Override
	public List<BmsBusiness> selectBusinessNoAgencyNoList(BmsBusiness bmsBusiness) {
		return bmsBusinessMapper.selectBusinessNoAgencyNoList(bmsBusiness);
	}

	@Override
	public List<BmsBusiness> selectBusinessNoCostNoList(BmsBusiness bmsBusiness) {
		List<BmsBusiness> list = bmsBusinessMapper.selectBusinessNoCostNoList(bmsBusiness);
		if (list != null && list.size() > 0) {
			Iterator<BmsBusiness> iterator = list.iterator();

			while (iterator.hasNext()) {
				BmsBusiness item = iterator.next();
				String nowReportType = item.getNowReportType();
				String oldReportType = item.getOldReportType();
				if (oldReportType != null && !"".equals(oldReportType)) {
					if (nowReportType != null && !"".equals(nowReportType)) {
						List<String> difflist = new ArrayList<String>();
						String[] oldList = oldReportType.split(";");
						String[] nowList = nowReportType.split(";");

						List<String> lists = Arrays.asList(nowList);

						Arrays.sort(oldList);
						Arrays.sort(nowList);
						if (Arrays.equals(oldList, nowList)) {
							iterator.remove();
						}

						for (String a : oldList) {
							// 判断是否包含
							if (!lists.contains(a)) {
								difflist.add(a);
							}
						}
						item.setReportTypes(difflist);
					} else {
						String[] oldList = oldReportType.split(";");
						item.setReportTypes(Arrays.asList(oldList));
					}
				} else {
					// 如果没有报告类型，则过滤掉
					iterator.remove();
				}
			}
			return list;
		} else {
			return list;
		}
	}

	/**
	 * 查询业务清单，通过当前登录人判断是否过滤招标代理类业务
	 *
	 * @param bmsBusiness
	 * @return
	 */
	@Override
	public List<BmsBusiness> selectProjectBusinessList(BmsBusiness bmsBusiness) {
		return bmsBusinessMapper.selectProjectBusinessList(bmsBusiness);
	}

	@Override
	public List<BmsMdBusinessCategoryVO> selectBusinessTypes(String projectId) {
		Set<String> serviceAreaSet = getBusinessTypeIds(projectId);
		System.out.println(serviceAreaSet);

		List<BmsMdBusinessCategoryVO> listsBmsMdBusinessCategories = new ArrayList<BmsMdBusinessCategoryVO>();
		if (serviceAreaSet.size() > 0) {
			for (String businessTypeId : serviceAreaSet) {

				BmsMdBusinessCategoryVO initResult = bmsMdBusinessCategoryMapper
						.selectBmsMdBusinessCategoryByGuIdNew(businessTypeId);
				// 1、查询本业务类别的所有父级类别
				List<BmsMdBusinessCategoryVO> selectBusinessType1 = bmsMdBusinessCategoryMapper
						.selectBusinessCategoryListsNew(businessTypeId);
				if (selectBusinessType1 != null && selectBusinessType1.size() > 0) {
					List<BmsMdBusinessCategoryVO> selectBusinessType2 = bmsMdBusinessCategoryMapper
							.selectBusinessCategoryListsNew(selectBusinessType1.get(0).getGuid());
					if (selectBusinessType2 != null && selectBusinessType2.size() > 0) {
						listsBmsMdBusinessCategories.add(initResult);
						listsBmsMdBusinessCategories.add(selectBusinessType1.get(0));
						listsBmsMdBusinessCategories.add(selectBusinessType2.get(0));
					}
				}
			}
		}

		Set<BmsMdBusinessCategoryVO> businessSet = new HashSet<>(listsBmsMdBusinessCategories);
		List<BmsMdBusinessCategoryVO> list = new ArrayList<>(businessSet);
		System.out.println(list);
		return list;
	}

	public Set<String> getBusinessTypeIds(String projectId) {
		// 1、获取本项目下的服务范围
		// 2、判断有哪些大类服务范围，然后找到各小类
		// 3、根据各大类+小类从服务范围对照表里取出对应的业务类别数据
		// 4、返回业务类别信息

		Set<String> serviceAreaSet = new HashSet<String>();
		// 1、获取本项目下的服务范围
		if (projectId != null && !"".equals(projectId)) {
			BmsProject bmsProject = bmsProjectMapper.selectBmsProjectByGuid(projectId);
			/*try {
			String serviceContent = ReflectUtils
					.getValueOfGetIncludeObjectFeild(bmsProject, res.getDictValue()).toString();
			if (serviceContent != null && !"".equals(serviceContent)) {
				String[] resSec = serviceContent.split(",");
				for (String string2 : resSec) {
					// 获取服务类别对应的业务类别
					BmsServiceArea sec = bmsServiceAreaMapper.selectBmsServiceArea(res.getDictValue(),
							string2);
					if (sec != null && sec.getBusiness() != null && !"".equals(sec.getBusiness())) {
						String[] resSec2 = sec.getBusiness().split(",");
						for (String string3 : resSec2) {
							serviceAreaSet.add(string3);
						}
					}

				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}*/

			if(bmsProject != null) {
				if (bmsProject.getServiceType() != null && !"".equals(bmsProject.getServiceType())) {
					// 如果服务类别有值，再去拿出来在数据字典里判断
					String[] arr = bmsProject.getServiceType().split(",");
					for (String string : arr) {
						// 获取本服务类别的数据字典
						SysDictData res = sysDictDataMapper.getServiceDictData(string);
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("service_type", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}

				if (StringUtils.isNotBlank(bmsProject.getEngineeringConsultingService())) {
					//前期咨询服务内容
					String[] arr1 = bmsProject.getEngineeringConsultingService().split(",");
					for (String string : arr1) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("engineeringConsultingService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getBiddingAgencyService())) {
					//招标代理服务内容
					String[] arr2 = bmsProject.getBiddingAgencyService().split(",");
					for (String string : arr2) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("biddingAgencyService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getCostConsultingService())) {
					//造价咨询服务内容
					String[] arr3 = bmsProject.getCostConsultingService().split(",");
					for (String string : arr3) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("costConsultingService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getProjectManagementService())) {
					//项目管理服务内容
					String[] arr4 = bmsProject.getProjectManagementService().split(",");
					for (String string : arr4) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("projectManagementService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getProjectSupervisionService())) {
					//建设监理服务内容
					String[] arr5 = bmsProject.getProjectSupervisionService().split(",");
					for (String string : arr5) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("projectSupervisionService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getEquipmentSupervisionService())) {
					//设备监理服务内容
					String[] arr6 = bmsProject.getEquipmentSupervisionService().split(",");
					for (String string : arr6) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("equipmentSupervisionService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getTestingService())) {
					//检测服务服务内容
					String[] arr7 = bmsProject.getTestingService().split(",");
					for (String string : arr7) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("testingService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getEiaQualificationService())) {
					//环境咨询服务内容
					String[] arr8 = bmsProject.getEiaQualificationService().split(",");
					for (String string : arr8) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("eiaQualificationService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getOtherConsultingTechnicalService())) {
					//其它咨询服务内容
					String[] arr9 = bmsProject.getOtherConsultingTechnicalService().split(",");
					for (String string : arr9) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("otherConsultingTechnicalService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getInformationConsultingService())) {
					//信息咨询服务内容
					String[] arr10 = bmsProject.getInformationConsultingService().split(",");
					for (String string : arr10) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("informationConsultingService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getSpecialConsultingService())) {
					//专项咨询服务内容
					String[] arr11 = bmsProject.getSpecialConsultingService().split(",");
					for (String string : arr11) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("specialConsultingService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
				if (StringUtils.isNotBlank(bmsProject.getDesignConsultingService())) {
					//设计咨询服务内容
					String[] arr12 = bmsProject.getDesignConsultingService().split(",");
					for (String string : arr12) {
						// 获取服务类别对应的业务类别
						BmsServiceArea main = bmsServiceAreaMapper.selectBmsServiceArea("designConsultingService", string);
						if (main != null && main.getBusiness() != null && !"".equals(main.getBusiness())) {
							String[] resMain = main.getBusiness().split(",");
							for (String string2 : resMain) {
								serviceAreaSet.add(string2);
							}
						}
					}
				}
			}

		}

		return serviceAreaSet;
	}

	@Override
	public int checkProject(String projectId,String businessId) {
		Set<String> serviceAreaSet = getBusinessTypeIds(projectId);
		BmsBusiness business = bmsBusinessMapper.selectBmsBusinessByGuid(businessId);
		boolean resFlag = serviceAreaSet.contains(business.getBusinessCategoryId());
		if(resFlag) {
			return 1;
		}else {
			return 0;
		}
	}

	@Override
	public int checkBusiness(String projectId,String businessTypeId) {
		Set<String> serviceAreaSet = getBusinessTypeIds(projectId);
		boolean resFlag = serviceAreaSet.contains(businessTypeId);
		if(resFlag) {
			return 1;
		}else {
			return 0;
		}

	}

	@Override
	public int checkBussinessNameSingle(BmsBusiness bmsBusiness) {
		List<BmsBusiness> resList = bmsBusinessMapper.checkBussinessNameSingle(bmsBusiness);
		if(resList!=null && resList.size()>0) {
			return 1;
		}else {
			return 0;
		}
	}


	public static void main(String[] args) {
		BmsBusiness bmsBusiness = new BmsBusiness();
		bmsBusiness.setGuid("aaaaaaa");
		bmsBusiness.setName("bbbbbbb");
		System.out.println("转换后字符串："+String.valueOf(bmsBusiness));
	}

	@Override
	public String getReportType(String businessTypeId) {
		return bmsBusinessMapper.getReportType(businessTypeId);
	}

	@Override
	public BmsProjectBusinessDto selectNewAndOldProjectInfo(String newProjectId, String oldProjectId,String businessId) {
		List<BmsReportNo> reportList = new ArrayList<BmsReportNo>();
		//获取该业务下的所有报告号/招标号
		List<BmsReportNo> reportResult = bmsBusinessMapper.getReportInfoByBusinessId(businessId);
		if(reportResult!=null && reportResult.size()>0) {
			for (BmsReportNo item : reportResult) {
				if(StringUtils.isNotBlank(item.getReportNo())) {
					reportList.add(item);
				}
			}
		}
		BmsProjectBusinessDto res = bmsBusinessMapper.selectNewAndOldProjectInfo(newProjectId, oldProjectId);
		if(res!=null && reportList!=null && reportList.size()>0) {
			res.setBmsReport(reportList);
		}
		return res;
	}

	@Override
	public BmsBusiness getReportTypeCounts(String businessTypeId) {
		return bmsBusinessMapper.getReportTypeCounts(businessTypeId);
	}

	@Override
	public int updateBmsBusinessWorkflow(BmsBusiness bmsBusiness) {
		return bmsBusinessMapper.updateBmsBusiness(bmsBusiness);
	}

	@Override
	public int updateWrittenStatus(BmsBusiness bmsBusiness) {
		return bmsBusinessMapper.updateWrittenStatus(bmsBusiness);
	}

	@Override
	public int updateMoveStatus(BmsBusiness bmsBusiness) {
		return bmsBusinessMapper.updateMoveStatus(bmsBusiness);
	}

	@Override
	public Integer getBusinessCount(String projectId) {
		return bmsBusinessMapper.getBusinessCount(projectId);
	}

	@Transactional
	@Override
	public int cancelBusiness(BmsBusiness bmsBusiness) {
		if(StringUtils.isNotBlank(bmsBusiness.getCancelStatus()) && StringUtils.isNotBlank(bmsBusiness.getGuid())) {
			BmsBusiness oldBusiness = bmsBusinessMapper.selectBmsBusinessByGuid(bmsBusiness.getGuid());
			BmsMdBusinessCategory businessType = bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryByGuId(bmsBusiness.getBusinessCategoryId());
			String businessName = bmsBusiness.getName();
			if(businessType!=null && businessType.getReportNoApply()!=null) {
				String reportNoApply = businessType.getReportNoApply();
				if(StringUtils.isNotBlank(reportNoApply)) {
					String eCStatus = "";
					if("1".equals(bmsBusiness.getCancelStatus())) {
						eCStatus="作废";

						boolean flagRes = false;
						//判断是否是历史报告号
						if("2".equals(businessType.getReportNoApply())) {//招标代理
							BmsAgencyNo bmsAgencyNo = new BmsAgencyNo();
							bmsAgencyNo.setAgencyNo(bmsBusiness.getAgencyNo());
							BmsAgencyNo res = bmsAgencyNoMapper.selectBmsAgencyNoHistoryByAgencyNo(bmsAgencyNo);
							if(res!=null) { //是历史数据
								flagRes = true;
							}else { //不是历史数据
								flagRes = false;
							}
						}else if("3".equals(businessType.getReportNoApply())) {//造价咨询
							BmsCostNo bmsCostNo = new BmsCostNo();
							bmsCostNo.setCostNo(bmsBusiness.getAgencyNo());
							BmsCostNo res = bmsCostNoMapper.selectBmsCostNoHistoryByCostNo(bmsCostNo);
							if(res!=null) { //是历史数据
								flagRes = true;
							}else { //不是历史数据
								flagRes = false;
							}
						}

						if(flagRes) {
							//int deleteRes = bmsBusinessMapper.deleteBmsBusinessByGuid(bmsBusiness.getGuid());
							//log.info("删除业务结果=="+deleteRes);
							if("2".equals(businessType.getReportNoApply())) {//是招标历史数据
								//删除招标代理号表内数据
								bmsAgencyNoMapper.deleteBmsAgencyNoByAgencyNo(bmsBusiness.getAgencyNo());
								//修改招标代理号历史表内状态
								bmsAgencyNoMapper.updateBmsAgencyNoHistoryByAgencyNoNew(bmsBusiness.getAgencyNo());
							}

							if("3".equals(businessType.getReportNoApply())) {//是造价历史数据
								//删除造价报告号表内数据
								bmsCostNoMapper.deleteBmsCostNoByCostNo(bmsBusiness.getAgencyNo());
								//修改造价报告号历史表内状态
								bmsCostNoMapper.updateBmsCostNoHistoryByCostNoNew(bmsBusiness.getAgencyNo());
							}

							//添加操作记录
							BmsBusinessOper bmsBusinessOper = new BmsBusinessOper();
							bmsBusinessOper.setOperType("7");
							bmsBusinessOper.setBusinessId(bmsBusiness.getGuid());
							bmsBusinessOper.setInitParams(bmsBusiness.getAgencyNo());
							bmsBusinessOper.setNewParams("");
							bmsBusinessOper.setOperPerson(SecurityUtils.getLoginUser().getUser().getNickName());
							bmsBusinessOper.setOperPersonNo(SecurityUtils.getLoginUser().getUser().getJobNo());
							bmsBusinessOper.setProjectId(bmsBusiness.getProjectId());
							bmsBusinessOper.setFieldName("报告号/招标号");
							bmsBusinessOper.setBusinessName(oldBusiness.getName());
							bmsBusinessOper.setBusinessNo(oldBusiness.getSectionNumber());
							bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);
						}

					}
					if("1".equals(reportNoApply)) {
						reportNoDao.updateReportNoStatus(bmsBusiness.getGuid(), eCStatus);
						if("作废".equals(eCStatus)) {
							bmsReportNoMapper.updateReportByBusinessIdCancel(bmsBusiness.getGuid());
						}else {
							bmsReportNoMapper.updateReportByBusinessId(bmsBusiness.getGuid());
						}
					}else if("2".equals(reportNoApply)) {
						agencyNoDao.updateAgencyNoStatus(bmsBusiness.getGuid(), eCStatus);
						if("作废".equals(eCStatus)) {
							bmsAgencyNoMapper.updateAgencyByBusinessIdCancel(bmsBusiness.getGuid());
						}else {
							bmsAgencyNoMapper.updateAgencyByBusinessId(bmsBusiness.getGuid());
						}
					}else if("3".equals(reportNoApply)) {
						costNoDao.updateCostNoStatus(bmsBusiness.getGuid(), eCStatus);
						if("作废".equals(eCStatus)) {
							bmsCostNoMapper.updateCostByBusinessIdCancel(bmsBusiness.getGuid());
						}else {
							bmsCostNoMapper.updateCostByBusinessId(bmsBusiness.getGuid());
						}
					}
				}
			}
			if("1".equals(bmsBusiness.getCancelStatus())) {
				bmsBusiness.setName("(作废)"+businessName);
			}else {
				if(StringUtils.isNotBlank(businessName) && businessName.length()>4 && "(作废)".equals(businessName.substring(0, 4))) {
					bmsBusiness.setName(businessName.substring(4));
				}
			}

			int res = 0;
			//取消作废
			if("0".equals(bmsBusiness.getCancelStatus())) {
				res = bmsBusinessMapper.updateCancelStatus(bmsBusiness.getName(),"0",bmsBusiness.getGuid());

				//添加操作记录
				BmsBusinessOper bmsBusinessOper = new BmsBusinessOper();
				bmsBusinessOper.setOperType("5");
				bmsBusinessOper.setBusinessId(bmsBusiness.getGuid());
				//bmsBusinessOper.setInitParams(JSONArray.toJSON(oldBusiness).toString());
				bmsBusinessOper.setNewParams("取消作废");
				bmsBusinessOper.setOperPerson(SecurityUtils.getLoginUser().getUser().getNickName());
				bmsBusinessOper.setOperPersonNo(SecurityUtils.getLoginUser().getUser().getJobNo());
				bmsBusinessOper.setProjectId(bmsBusiness.getProjectId());
				bmsBusinessOper.setFieldName("作废状态");
				bmsBusinessOper.setBusinessName(oldBusiness.getName());
				bmsBusinessOper.setBusinessNo(oldBusiness.getSectionNumber());
				bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);
			}
			//作废
			if("1".equals(bmsBusiness.getCancelStatus())) {
				//添加操作记录
				BmsBusinessOper bmsBusinessOper = new BmsBusinessOper();
				bmsBusinessOper.setOperType("3");
				bmsBusinessOper.setBusinessId(bmsBusiness.getGuid());
				//bmsBusinessOper.setInitParams(JSONArray.toJSON(oldBusiness).toString());
				bmsBusinessOper.setNewParams("作废");
				bmsBusinessOper.setOperPerson(SecurityUtils.getLoginUser().getUser().getNickName());
				bmsBusinessOper.setOperPersonNo(SecurityUtils.getLoginUser().getUser().getJobNo());
				bmsBusinessOper.setProjectId(bmsBusiness.getProjectId());
				bmsBusinessOper.setFieldName("作废状态");
				bmsBusinessOper.setBusinessName(oldBusiness.getName());
				bmsBusinessOper.setBusinessNo(oldBusiness.getSectionNumber());
				bmsBusinessOperService.insertBmsBusinessOper(bmsBusinessOper);

				BmsWorkflow bmsWorkflow = new BmsWorkflow();
				bmsWorkflow.setBusinessId(bmsBusiness.getGuid());
				log.debug("业务作废时取消流程，业务id——————————————————-"+bmsBusiness.getGuid());
				bmsProjectService.cancelInstance(bmsWorkflow);
				res = bmsBusinessMapper.updateCancelStatus(bmsBusiness.getName(),"1",bmsBusiness.getGuid());
			}

			return 1;
		}else {
			return 0;
		}
	}

	@Override
	public String checkWorkflow(BmsBusiness bmsBusiness) {
		BmsMdBusinessCategory businessType = bmsMdBusinessCategoryMapper.selectBmsMdBusinessCategoryByGuId(bmsBusiness.getBusinessCategoryId());
		if(businessType!=null) {//&& StringUtils.isNotBlank(businessType.getReportNoApply())
			BmsWorkflow bmsWorkflow = new BmsWorkflow();
			bmsWorkflow.setBusinessId(bmsBusiness.getGuid());
			bmsWorkflow.setStatus("1");
			int resInt = 0;
			if("1".equals(businessType.getReportNoApply())) {//前期咨询
				bmsWorkflow.setSheetCode("ConsultingOutcomeDocument");
				List<BmsWorkflow> resList = bmsWorkflowMapper.selectBmsWorkflowList(bmsWorkflow);
				resInt = resList.size();
			}else if("2".equals(businessType.getReportNoApply())) {//招标代理
				bmsWorkflow.setSheetCode("biddingDocument");
				List<BmsWorkflow> resList = bmsWorkflowMapper.selectBmsWorkflowList(bmsWorkflow);
				resInt = resList.size();
			}else if("3".equals(businessType.getReportNoApply())) {//造价咨询
				bmsWorkflow.setSheetCode("costConsultingReportSeal");
				List<BmsWorkflow> resList = bmsWorkflowMapper.selectBmsWorkflowList(bmsWorkflow);
				resInt = resList.size();
			}else {
				bmsWorkflow.setSheetCode("supervisionPlanningReport");//监理规划报审
				List<BmsWorkflow> resList1 = bmsWorkflowMapper.selectBmsWorkflowList(bmsWorkflow);
				bmsWorkflow.setSheetCode("projectManageResultsReview");//项目管理成果文件报审
				List<BmsWorkflow> resList2 = bmsWorkflowMapper.selectBmsWorkflowList(bmsWorkflow);

				resInt = resList1.size()+resList2.size();
			}

			if(resInt>0) {//有成果文件盖章流程
				return "1";
			}else {//无成果文件盖章流程
				return "2";
			}
		}
		return "0";
	}

	@Override
	public void cancelInstance(String businessId) {
		BmsWorkflow bmsWorkflow = new BmsWorkflow();
		bmsWorkflow.setBusinessId(businessId);
		log.debug("业务作废时取消流程，业务id——————————————————-"+businessId);
		bmsProjectService.cancelInstance(bmsWorkflow);
	}
}
